package com.sikadwafastdaisfondturbo.pretloan.utils.http

import com.sikadwa.syc.http.HttpRequestBuilder
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.event.track.TrackEventManager
import com.sikadwafastdaisfondturbo.pretloan.utils.log.HHLog
import com.sikadwafastdaisfondturbo.pretloan.utils.log.JsonFormat
import org.json.JSONObject
import java.io.File


/**
 * Sikadwa
 *
 * @Author wwdeng
 * @CreateTime: 2024/5/20 18:04
 * @Annotation：
 */
class OkHttpLoaderV2 : IHttpLoader {

    /**
     * get
     */
    override fun get(url: String, callback: IHttpCallback) {
        HttpRequestBuilder()
            .url(url)
            .method("GET")
            .addHeader("product", Configs.AppName)
            .addHeader("appid", Configs.AppName)
//            .body(body)
            .timeouts(20000, 20000)
            .execute(object : HttpRequestBuilder.Callback {
                override fun onSuccess(response: String) {
                    val result = AESCryptUtil.decryptDefault(response)
                    if (result.isNotEmpty()){
                        Thread {
                            HHLog.i("data：" + JsonFormat.format(result))
                        }.start()
                        callback.onRequestSuccess(JSONObject(result))
                    }
                }
                override fun onFailure(code: Int, msg: String?) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode",code)
                    errorObj.put("errorMsg",msg)
                    callback.onRequestError(errorObj)
                }
                override fun onFinished() {
                    callback.onRequestFinish()
                }
            });
    }

    /**
     * post
     */
    override fun post(url: String, params: JSONObject, callback: IHttpCallback) {
        val aesParams = AESCryptUtil.encryptDefault(params.toString())
        HHLog.e("HttpUrl：  $url")
        HHLog.e("HttpParams： $params")
        HHLog.e("HttpParams AES：  $aesParams")
        HttpRequestBuilder()
            .url(url)
            .method("POST")
            .addHeader("product", Configs.AppName)
            .addHeader("appid", Configs.AppName)
            .body(aesParams)
            .timeouts(20000, 20000)
            .execute(object : HttpRequestBuilder.Callback {
                override fun onSuccess(response: String) {
                    val result = AESCryptUtil.decryptDefault(response)
                    if (result.isNotEmpty()){
                        Thread {
                            HHLog.i("data：" + JsonFormat.format(result))
                        }.start()
                        callback.onRequestSuccess(JSONObject(result))
                    }
                }
                override fun onFailure(code: Int, msg: String?) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode",code)
                    errorObj.put("errorMsg",msg)
                    callback.onRequestError(errorObj)
                }
                override fun onFinished() {
                    callback.onRequestFinish()
                }
            });
    }

    /**
     * 添加埋点
     */
    override fun addEvent(eventName: String, remark: String?) {
        var eventType = eventName.uppercase()//转大写
        if(!eventType.startsWith("SIKADWA_")){
            eventType = "SIKADWA_$eventType"
        }
        TrackEventManager.trackEvent(eventType,remark)

//        val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
//        val params = JSONObject().addCommonParams().apply {
//            put("path", AppUrls.PATH.TRACK_EVENT)
//            put("type",eventType)//事件类型
//            put("remark",remark)//事件备注
//        }
//        val aesParams = AESCryptUtil.encryptDefault(params.toString())
//        HHLog.e("event url：  $httpUrl")
//        HHLog.e("event params： $params")
//        HHLog.e("event params aes：  $aesParams")
//        HttpRequestBuilder()
//            .url(httpUrl)
//            .method("POST")
//            .addHeader("product", Configs.AppName)
//            .addHeader("appid", Configs.AppName)
//            .body(aesParams)
//            .timeouts(20000, 20000)
//            .execute(object : HttpRequestBuilder.Callback {
//                override fun onSuccess(response: String) {
//                    val result = AESCryptUtil.decryptDefault(response)
//                    if (result.isNotEmpty()){
//                        Thread {
//                            HHLog.i("event result data：" + JsonFormat.format(result))
//                        }.start()
//                    }
//                }
//                override fun onFailure(code: Int, msg: String?) {
//                }
//                override fun onFinished() {
//                }
//            });
    }

    /**
     * post upload file
     */
    override fun uploadFile(url: String, file: File, callback: IHttpCallback) {
        HttpRequestBuilder()
            .url(url)
            .method("POST")
            .addHeader("product", Configs.AppName)
            .addHeader("appid", Configs.AppName)
            .timeouts(20000, 20000)
            .uploadFile(file.path,object : HttpRequestBuilder.Callback {
                override fun onSuccess(response: String?) {
                    if (response.isNullOrEmpty()){
                        val errorObj = JSONObject()
                        errorObj.put("errorCode", -1)
                        errorObj.put("errorMsg", "http request error")
                        callback.onRequestError(errorObj)
                    } else {
                        Thread {
                            HHLog.i("url：$url --> " + JsonFormat.format(response))
                        }.start()
                        val data = JSONObject(response)
                        callback.onRequestSuccess(data)
                    }
                }

                override fun onFailure(code: Int, msg: String?) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode", code)
                    errorObj.put("errorMsg", msg)
                    callback.onRequestError(errorObj)
                }

                override fun onFinished() {
                    callback.onRequestFinish()
                }
            });
    }
}
